export const getTemplate = function (getObj: any) {
  console.log(getObj);
  let tableItem = "";
  let formItem = "";
  let ruleItem = "";
  let searchForm = "";
  getObj.fields.forEach(item => {
    tableItem += `
    <el-table-column prop="${item.field}" label="${item.description}" />
    `
    formItem += `
    <el-form-item label="${item.description}" prop="${item.field}">
      <el-input v-model="formData.${item.field}" placeholder="请输入${item.description}" />
    </el-form-item>`
    ruleItem += `
    ${item.field}: [{ required: true, message: "请输入${item.description}", trigger: "blur" }],
    `
    searchForm += `<el-form-item label="${item.description}">
          <el-input v-model="searchForm.${item.field}s" placeholder="请输入${item.description}" />
        </el-form-item>`
  });

  return `
    <template>
    <div class="container-box">
      <CommonCrud ref="crudRef" @search="handleSearch">
        <!-- 工具栏按钮 -->
        <template #table-header>
          <el-button v-permission="'system:${getObj.modelName}:add'" type="primary" @click="handleAdd">
            新增
          </el-button>
        </template>
      <template #search-form="{ searchForm }">
        <el-form-item label="用户名">
          <el-input v-model="searchForm.username" placeholder="请输入用户名" />
        </el-form-item>
      </template>
        ${tableItem}
        <template #operation="{ row }">
          <el-button v-permission="'system:${getObj.modelName}:edit'" link type="primary" @click="handleEdit(row)">
            编辑
          </el-button>
          <el-button v-permission="'system:${getObj.modelName}:del'" link type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </CommonCrud>
      <CommonDialog v-model="dialogVisible" :title="title" width="500px" @confirm="handleSubmit" @cancel="handleDialogClose"
        @close="handleDialogClose">
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
          ${formItem}
        </el-form>
      </CommonDialog>
      </div>
    </template>
      < script setup lang = "ts" >
      import { ref, reactive, onMounted } from "vue"
      import type { FormInstance } from "element-plus"
      import CommonCrud from "@/components/CommonCrud/index.vue"
      import {
        get${getObj.modelName}List,
        add${getObj.modelName},
        edit${getObj.modelName},
        delete${getObj.modelName},
      } from "@/api/${getObj.modelName}"
      import { ElMessage, ElMessageBox } from "element-plus"
      const crudRef = ref()
      const dialogVisible = ref(false)
      const title = ref("")
      const formRef = ref()
      const formData = ref({
      })

      // 表单校验规则
      const rules = {
        ${ruleItem}
      }
        // 查询
const handleSearch = async (params: any) => {
  crudRef.value.loading = true
  try {
    const res: any = await get${getObj.modelName}List(params)
    if (res.code === 200) {
      crudRef.value.tableData = res.data.data
      crudRef.value.total = res.data.total
    }
  } finally {
    crudRef.value.loading = false
  }
}
      // 提交表单
      const handleSubmit = async () => {
        if (title.value === "新增") {
          const res = await add${getObj.modelName}(formData.value)
          if (res.code === 200) {
            ElMessage.success("添加成功")
            crudRef.value.handleSearch()
            handleDialogClose()
          }
        } else if (title.value === "编辑") {
          const res = await edit${getObj.modelName}(formData.value.id, formData.value)
          if (res.code === 200) {
            ElMessage.success("保存成功")
            crudRef.value.handleSearch()
            handleDialogClose()
          }
        }
      }
      // 关闭弹窗
      const handleDialogClose = () => {
        dialogVisible.value = false
        formRef.value.resetFields()
      }
      // 编辑
      const handleEdit = (row) => {
        dialogVisible.value = true
        title.value = "编辑"
        formData.value = { ...row }
      }
      // 新增
      const handleAdd = () => {
        dialogVisible.value = true
        title.value = "新增"
      }
      // 删除
      const handleDelete = async (row) => {
        ElMessageBox.confirm("确定要删除吗?", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(async () => {
            const res = await delete${getObj.modelName}(row.id)
            if (res.code === 200) {
              ElMessage.success("删除成功")
              crudRef.value.handleSearch()
            }
          })
          .catch(() => {
            ElMessage({
              type: "info",
              message: "删除取消",
            })
          })
      }


      onMounted(() => {
        handleSearch({})
      })
    </script>

    <style scoped lang = "scss" > </style>

      `
}
